package com.supreme.omsserver.component;

import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class BloomFilterService {

    @Resource
    private RedissonClient redissonClient;

    private final Map<String, RBloomFilter<Object>> bloomFilterMap = new ConcurrentHashMap<>();

    public RBloomFilter<Object> getBloomFilter(String key, int capacity, double errorRate) {
        return Optional.ofNullable(bloomFilterMap.get(key)).orElseGet(() -> {
            RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(key);
            boolean tryInit = bloomFilter.tryInit(capacity, errorRate);
            System.out.println("tryInit = " + tryInit);
            return bloomFilter;
        });
    }
}
